package handler;

import http.MyHttpRequest;

import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Karl
 * @Date 2023/1/2 - 14:13
 */
public class HandlerManager {

    public static List<Handler> handlers;

    public static Handler defaultHandler = new ForwardHandler();

    static {
        handlers = new ArrayList<>();
        handlers.add(new ConnectHandler());             //处理https连接
        handlers.add(new AuthenticateHandler());        //处理需要验证的网站
        handlers.add(new FilterHandler());              //处理需要过滤的网站
        handlers.add(new CacheHandler());               //处理代理服务器有缓存的请求
        handlers.add(defaultHandler);                   //处理默认转发，并缓存
    }

    public static Handler getHandlerAdapter(MyHttpRequest request, Socket remote){
        if (handlers != null) {
            for (Handler adapter : handlers) {
                if (adapter.isSupport(request,remote)) {
                    return adapter;
                }
            }
        }

        return defaultHandler;
    }

}
